package com.jt.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemCatMapper;
import com.jt.mapper.ItemCatParamMapper;
import com.jt.pojo.ItemCatParam;
import com.jt.service.ItemCatParamService;
import com.jt.utils.Assert;
import com.jt.vo.EasyUITable;
import com.jt.vo.ItemParamVo;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author dc
 * @Classname ItemParamServiceImpl
 * @Date 2020/2/27 10:02
 */
@Service
public class ItemCatParamServiceImpl implements ItemCatParamService {

    @Autowired
    private ItemCatParamMapper itemCatParamMapper;

    @Autowired
    private ItemCatMapper itemCatMapper;

    @Override
    public EasyUITable findItemCatParam(Integer page, Integer rows) {
        Assert.isValid(page<1,"请输入正确的页码值");
        Assert.isValid(rows<1,"每页至少有一条查询记录");
        IPage<ItemCatParam> pages = new Page<>(page,rows);
        QueryWrapper<ItemCatParam> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("updated");
        IPage<ItemCatParam> itemParamIPage = itemCatParamMapper.selectPage(pages, queryWrapper);
        List<ItemCatParam> rs = itemParamIPage.getRecords();
        List<ItemParamVo> records=new ArrayList<>();
        for(ItemCatParam record:rs){
            String name = itemCatMapper.selectById(record.getItemCatId()).getName();
            ItemParamVo vo = new ItemParamVo();
            vo.setItemCatName(name).setId(record.getId()).setItemCatId(record.getItemCatId())
                    .setParamData(record.getParamData()).setUpdated(record.getUpdated())
                    .setCreated(record.getCreated());
            records.add(vo);
        }
        int Rowcount = (int)itemParamIPage.getTotal();
        return new EasyUITable(Rowcount,records);
    }

    @Override
    public SysResult getItemCatParamByCid(Long cid) {
        QueryWrapper<ItemCatParam> wrapper = new QueryWrapper<>();
        wrapper.eq("item_cat_id",cid);
        ItemCatParam param = itemCatParamMapper.selectOne(wrapper);
        SysResult rs=param!=null?SysResult.success(200,param):SysResult.fail();
        return rs;
    }

    @Override
    public void saveItemCatParam(String paramData,Long cid) {
        ItemCatParam entity = new ItemCatParam();
        entity.setParamData(paramData).setItemCatId(cid).setCreated(new Date()).setUpdated(entity.getCreated());
        itemCatParamMapper.insert(entity);
    }
}
